package com.itheima.college_infomaneger_sys.controller;

import com.itheima.college_infomaneger_sys.domain.Student;
import com.itheima.college_infomaneger_sys.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private static Scanner sc = new Scanner(System.in);
    private static StudentService ss = new StudentService();

    public static void start() {
        int choice;
        stu_loop:
        while (true) {
            System.out.println("------欢迎登录学生信息管理系统------");
            System.out.println("请选择你要进行的操作:\r\n1.添加学生信息  2.查询学生信息  3.修改学生信息  4.删除学生信息  5.返回上一级");
            choice = sc.nextInt();
            switch (choice) {
                case 1:
//                    添加学生信息
                    addStudent();
                    break;
                case 2:
//                    查询学生信息
                    getAllStudents();
                    break;
                case 3:
                    System.out.println("3");
                    break;
                case 4:
//                    删除学生信息
                    delStudentInfo();
                    break;
                case 5:
                    break stu_loop;
                default:
                    System.out.println("你的输入有误,请重新输入");
            }
        }
    }

    private static void delStudentInfo() {
        boolean isEmpty = ss.isEmpty();
        if(isEmpty){
            System.out.println("数据库中暂无学生信息!");
            return;
        }String id = getTargetId();
        ss.delStudentInfo(id);
    }

    private static String getTargetId(){
        String id;

        while (true){
            System.out.println("请输入学生学号:");
            id = sc.next();
            boolean exist = ss.isExist(id);
            if (exist) {
                return id;
            }
            System.out.println("你输入的学号不存在!");
        }
    }

    private static void getAllStudents() {
        ss.getAllStudents();
    }

    public static void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生学号:");
            id = sc.next();
            boolean exist = ss.isExist(id);
            if (!exist) {
                break;
            }
            System.out.println("你输入的信息已存在!");
        }
        Student stu = getStudentInfo(id);
        boolean result = StudentService.addStudent(stu);

        if (result) {
            System.out.println("添加成功!");
        } else {
            System.out.println("容量已满,添加失败!");
        }
    }

    public static Student getStudentInfo(String id) {
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生生日:");
        String birthday = sc.next();

        Student stu = new Student(id, name, age, birthday);
        return stu;
    }
}
